﻿<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<jsp:include page="../inc.jsp"></jsp:include>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户管理主页</title>
</head>
<body>
<script type="text/javascript" src="${ctx}/js/export.js"></script>
<script type="text/javascript">
	var dataGrid;
	$(function(){
		dataGrid=$('#dg').datagrid({    
		    url:'${ctx}/user/find',  
		    fit:true,//满屏
		    singleSelect:true,//单行选中
		    pagination:true,//翻页效果
		    pageSize:20,//一页多少记录
		    pageList:[10,20,30,50],
		    rownumbers:true,//显示行号 
		    toolbar: [{
				iconCls: 'icon-eadd',
				text:'新&nbsp;&nbsp;增',
				width:80,
				height:30,
				handler: function(){
					openAddDia('新增用户','');
				}
			},'-',{
				iconCls: 'icon-eedit',
				text:'修&nbsp;&nbsp;改',
				width:80,
				height:30,
				handler: function(){
					debugger;
					//获取选中记录的数据
					var row = $("#dg").datagrid('getSelected');
					if(row==null){
						alert("请选择要修改的记录");
						return;
					}
					//把对象转换成字符串
					var js = JSON.stringify(row);
					openAddDia('修改用户',js);
				}
			},'-',{
				text:'删&nbsp;&nbsp;除',
				iconCls:'icon-remove',
				width:80,
				height:30,
				handler:function(){
					//获取选中记录的数据
					var row = $("#dg").datagrid('getSelected');
					if(row==null){
						alert("请选择要删除的记录");
						return;
					}
					//弹出确认框
					$.messager.confirm('删除确认','真的要删除该记录?',function(r){
						if(r){//r=true
							$.ajax({
								type:'get',
								url:'${ctx}/user/delete/'+row.id,
								dataType:'json',
								success:function(data){
									alert(data.msg);
									if(data.sus){
										//表格自动刷新
										$('#dg').datagrid('reload');
									}
								}
							});
						}
					});
				}
			},'-',{
				text:'导出excel',
				iconCls:'icon-redo',//自定义的css样式图片
				handler: function(){
					 /* var url = "${ctx}/sc/export";
					expt(dataGrid,url,'用户信息'); */
					var rows = dataGrid.datagrid("getRows");
					//console.log("row\n"+JSON.stringify(rows));
					export_v1(rows);
				}
			},'-',{
				text:'导入excel',
				iconCls:'icon-undo',
				width:100,
				height:30,
				handler:function(){
					parent.$.modalDialog({
						title:'',
						href:'${ctx}/sc/inExcel',
						cache:false,
						closable:false,
						width:350,
						height:100,
						buttons:[{
							text:'确&nbsp;&nbsp;定',
							iconCls:'icon-ok',
							width:80,
							height:30,
							handler:function(){
								inExcel();
							}
						},{
							text:'取&nbsp;&nbsp;消',
							iconCls:'icon-cancel',
							width:80,
							height:30,
							handler:function(){
								//关闭对话框
								parent.$.modalDialog.handler.dialog('close');
							}
						}]
					});					
				}
			}],
		    columns:[[    
		        {field:'loginName',title:'登录名',width:100},    
		        {field:'userName',title:'用户名',width:100},    
		        {field:'deptName',title:'部门名称',width:100},    
		        {field:'roleName',title:'角色名称',width:100},    
		        {field:'phone',title:'手机号码',width:100},    
		        {field:'createDate',title:'创建时间',width:100},    
		    ]] 
		}); 
		//刷新分页
		setTimeout(function(){
			$("#dg").datagrid({loadFilter:pagerFilter}).datagrid('loadData');
		}, 10);
		
		$('#searchBtn').click(function(){
			$.ajax({
				type:'post',
				url:'${ctx}/user/find',
				data:$('#searchForm').serialize(),
				dataType:'json',
				success:function(data){
					//重新加载数据
					$('#dg').datagrid('loadData',data);
				}
			});
		});
	});
	
	function inExcel(){
		//获取全屏弹出框的form对象
		var f = parent.$.modalDialog.handler.find('#formData');
		//判断是否完成必填或者格式验证(easyui)
		if(f.form('validate')){
			var option = {
				type:'post',//提交方式
				url:'${ctx}/user/inExcel',//提交路径
				dataType:'json',//提交成功或者失败返回值的数据格式(text,html,String)
				headers:{"ClientCallMode":"ajax"},//添加请求头部
				success:function(data){//提交成功完成后执行的方法
					alert(data.msg);
					if(data.sus){
						//关闭对话框
						parent.$.modalDialog.handler.dialog('close');
						//表格自动刷新
						$('#dg').datagrid('reload');
					}
				}
			};
			//执行ajax的提交
			f.ajaxSubmit(option);
		}
	}
	
	function openAddDia(title,js){
		parent.$.modalDialog({
			title:title,
			href:'${ctx}/user/aoe?js='+encodeURI(js),
			cache:false,
			closable:false,
			width:350,
			height:500,
			buttons:[{
				text:'保&nbsp;&nbsp;存',
				iconCls:'icon-save',
				width:80,
				height:30,
				handler:function(){	
				    var url = '${ctx}/user/insert';
					if(js!=''){
						url = '${ctx}/user/update';
					}
					saveUser(url);
				}
			},{
				text:'取&nbsp;&nbsp;消',
				iconCls:'icon-cancel',
				width:80,
				height:30,
				handler:function(){
					//全屏对话框的关闭
					parent.$.modalDialog.handler.dialog('close');
				}
			}]
		});
	
	}
	function saveUser(url){
		debugger;
		var f= parent.$.modalDialog.handler.find('#userForm');
		if(f.form('validate')){
			var value= f.serialize();
			$.ajax({
				type:'post',
				url:url,
				data:value,
				dataType:'json',
				success:function(data){
					alert(data.msg);
					if(data.sus){
						//全屏对话框的关闭
						parent.$.modalDialog.handler.dialog('close');
						//表格自动刷新
						$('#dg').datagrid('reload');
					}
				}
			});
		}
	}
	//2019-10-05 wrb 新的导出方法
	var export_v1 = function(value,url){
		/* $.ajax({
			type:'post',
			url:url+'/'+JSON.stringify(value),
			//data:{"data":value},
			//dataType:'json',
			success:function(result){
				alert(result.msg);
				parent.$.modalDialog.handler.dialog('close');
			}
		}); */
		var url = "sc/export_v2";
		debugger;
		var params = "userName="+ $("[name='userName']").val()+"&phone="+$("[name='phone']").val();
		window.open('../sc/export_v2?'+params);
	}
</script>
<div id="cc" class="easyui-layout" data-options="fit:true,border:false" style="padding:0px;background:#fff;">   
    <div data-options="region:'north',title:''" style="height:60px;">
		<form id="searchForm">
			<table class="table">
				<tr>
				  <td>用户名:</td>
				  <td>
				  	<input class="easyui-textbox" data-options="
				  		width:200,
				  		height:30
				  	" name="userName">
				  </td>
				  <td>电话号码:</td>
				  <td>
				  	<input class="easyui-textbox" data-options="
				  		width:200,
				  		height:30
				  	" name="phone">
				  </td>
				  <td>
				  	<a class="easyui-linkbutton" id="searchBtn"
				  	data-options="
				  		width:80,
				  		height:30,
				  		iconCls:'icon-search'
				  	">查&nbsp;&nbsp;询</a>
				  </td>
				</tr>
			</table>  
		</form>  
    </div>   
    <div data-options="region:'center',title:''" style="padding:0px;background:#fff;">
    	<table id="dg"></table> 
    </div>   
</div>
</body>
</html>